function [d, bc]=bhat_gauss(mu1,C1,mu2,C2)
% BHATTACHARYYA  Bhattacharyya distance and coef. between two Gaussian classes
%
% d = bhattacharyya(X1,X2) returns the Bhattacharyya distance and coef between X1 and X2.
%
%
% Output: d is the Bhattacharyya distance between these two sets of data
% Reference:
% Kailath, T., The Divergence and Bhattacharyya Distance Measures in Signal
% Selection, IEEE Trasnactions on Communication Technology, Vol. 15, No. 1,
% pp. 52-60, 1967

% %Check inputs and output
% error(nargchk(2,2,nargin));
% error(nargoutchk(0,1,nargout));

if length(mu1) == 1
    C=(C1+C2)/2;
    dmu=(mu1-mu2)/chol(C);
    try
        d=0.125*(dmu*dmu')+0.5*log(det(C/chol(C1*C2)));
        bc = exp(-1*d);
    catch
        d=0.125*(dmu*dmu')+0.5*log(abs(det(C/sqrtm(C1*C2))));
        bc = exp(-1*d);
        warning('MATLAB:divideByZero','Data are almost linear dependent. The results may not be accurate.');
    end
else
    
end
% d=0.125*dmu*dmu'+0.25*log(det((C1+C2)/2)^2/(det(C1)*det(C2)));